package com.dt76.smallloanbackenduser.controller.lhuaw;

import com.dt76.smallloanbackenduser.pojo.lhuaw.SystemMessage;
import com.dt76.smallloanbackenduser.remote.lhuaw.SystemMessagetest;
import com.dt76.smallloanbackenduser.utils.Page;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/lhuaw")
public class SystemMessageController {
    @Autowired
    private SystemMessagetest systemMessagetest;

@RequestMapping("/getAll")
public String SystemMessage1(Model model){
    model.addAttribute("SystemMessageList",systemMessagetest.getAll());
    System.out.println("调用成功");
    System.out.println(systemMessagetest.getAll());
    return "pages/lhuaw/SystemMessage";
}
@RequestMapping("/qryConditionPageAll")
public String qryConditionPageAll(Model model, Integer currentPageNo,
                                  @RequestParam(value ="messageTitle",required = false)String messageTitle,
                                  @RequestParam(value = "publisher",required = false)String publisher
                                  ){
   if (currentPageNo <= 0) {
        currentPageNo = 1;
   }
    String parammessageTitle= "";
    String parampublisher="";
    if(messageTitle != null && ! messageTitle.equals("")){
        parammessageTitle ="%"+messageTitle+"%";
    }
    if(publisher !=null && ! publisher.equals("")){
        parampublisher ="%"+publisher+"%";
    }
    Page page = systemMessagetest.qryConditionPageAll(currentPageNo,parammessageTitle,parampublisher);
    model.addAttribute("qrySystemMessageList",page.getRows());
    model.addAttribute("page",page);
    model.addAttribute("messageTitle",messageTitle);
    model.addAttribute("publisher",publisher);
    System.out.println(page.getRows());
    return "pages/lhuaw/SystemMessage";
}
    @RequestMapping("/delete/{sId}")
    @ResponseBody
    public Result delete(@PathVariable String sId){
        return systemMessagetest.delSystem(Integer.parseInt(sId));
    }
    @RequestMapping("/addSystem")
    @ResponseBody
    public Result addSystem(
            @RequestParam("messageTitle") String messageTitle,
            String sId,
            SystemMessage systemMessage) {
        Result result = new Result();
        if (sId.equals("0")) {
            systemMessage.setMessageTitle(messageTitle);
            //调用新增接口
            result = systemMessagetest.addSystem(systemMessage);

            if (result.isSuccess()) {
                //刷新全部的信息
                result.setMsg("添加成功");
            } else {
                result.setMsg("添加失败");
            }
        }
        return result;
    }
    @RequestMapping("/seeSystem/{sId}")
    @ResponseBody
    public SystemMessage seeSystem(@PathVariable String sId){
        //调用查询接口
        System.out.println(Integer.parseInt(sId));
        System.out.println(systemMessagetest.seeSystem(Integer.parseInt(sId)));
        return systemMessagetest.seeSystem(Integer.parseInt(sId));
    }

}
